package com.guchenbo.example.io;

import java.io.IOException;
import java.util.List;

/**
 * @author guchenbo
 * @date 2022/4/28
 */
public class GuFileDbTest {

    public static void main(String[] args) throws Exception {
        List<String> books = BookUtils.getBooks();
        String baseDir = "/tmp/gufiledb";
        GuFileDb gfd = new GuFileDb(baseDir);
        String subject = "book";

//        testWrite(gfd, subject, books);
        testRead(gfd, subject, books);
    }

    private static void testWrite(GuFileDb gfd, String subject, List<String> books) throws IOException {
        StringSerializer serializer = new StringSerializer();
        for (int i = 0; i < 3000; i++) {
            gfd.writeData(subject, books, serializer);
        }
        System.out.println("write done");
    }

    private static void testRead(GuFileDb gfd, String subject, List<String> books) throws Exception {
        int size = books.size();
        StringDeserializer deserializer = new StringDeserializer();
        for (int i = 0; i < 82582; i++) {
            int dataNo = i + 1;
            System.out.println(dataNo);
            String data = gfd.readData(subject, dataNo, deserializer);
            int idx = dataNo % size - 1;
            if (idx == -1) {
                idx += size;
            }
            String b = books.get(idx);
            System.out.println(data);
            System.out.println(b);

            if (!b.equals(data)) {
                break;
            }
        }
    }

}
